package com.springbootstudy.demo.web;

import com.springbootstudy.demo.entity.Return_User;
import com.springbootstudy.demo.entity.User;
import com.springbootstudy.demo.service.UserService;
import com.springbootstudy.demo.utils.IdWorker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


/**
 * web端用户登录 就那个网页登录的页面
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {
	// http://192.168.10.37:8080/Shopping_2018/Uesr/register
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	com.zhi.pojo.Return returns;


	@Autowired
	private UserService service;

	/**
	 * 用户注册
	 *
	 * @param request
	 * @param user_account
	 * @param user_password
	 * @return
	 * @throws IOException
	 */
//	 http://localhost:9700/User/register?user_account="111"&&user_password="222"&&user_name="333"
	@RequestMapping(value = "/register")
	@ResponseBody
	private com.zhi.pojo.Return register(HttpServletRequest request,
										 @RequestParam("user_account") String user_account,
										 @RequestParam("user_password") String user_password,
										 @RequestParam("user_name") String user_name) throws IOException {
		System.out.println("进来了" + user_account + user_password);
		// 查询这个账号的用户是否存在
		Integer number = service.getUserNumer(user_account);
		System.out.println("进来了" + number);
		//如果不存在
		if (number == 0) {

			System.out.println("没有注册，添加数据库");
			final IdWorker w = new IdWorker(1, 2);
			long l = w.nextId();
			//就添加数据库一条进去
			service.addUser(user_account, user_password, user_name);
			returns = new com.zhi.pojo.Return();
			returns.setCode(10001);
			returns.setIssuccess(true);
			returns.setMsg("注册成功！");
		} else {
			System.out.println("已经注册过了，请直接登录");

			returns = new com.zhi.pojo.Return();
			returns.setCode(10001);
			returns.setIssuccess(false);
			returns.setMsg("已经注册过了，请直接登录！");
		}

		return returns;
	}

	/**
	 * 管理员
	 *
	 * @param request

	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings("unused")

	@RequestMapping(value = "/login")
	@ResponseBody
	private com.zhi.pojo.Return login( HttpServletRequest request,  @RequestParam("user_account") String user_account,
										 @RequestParam("user_password") String user_password) throws IOException {
//		String user_account = loginBean.getAccount();
//		String user_password = loginBean.getPassword();
//		String Code = loginBean.getCode();
//		String attribute = (String) request.getSession().getAttribute("key");
//		if (!StringUtils.isEmpty(Code) && Code.equals(attribute)) {
//			// 验证码不一直
//		}else{
//
//			returns = new com.zhi.pojo.Return();
//			returns.setCode(10003);
//			returns.setIssuccess(false);
//
//			returns.setMsg("验证码不正确");
//			return returns;
//		}

//		System.out.println("进来了" + loginBean.getAccount()
//				+ loginBean.getPassword() + attribute);
		System.out.println("进来了" );
		// 是否存在
		Integer number = service.getUserNumer(user_account);
		if (number == 0) {
			System.out.println("没有注册，请先注册");
			returns = new com.zhi.pojo.Return();
			returns.setCode(10004);
			returns.setIssuccess(false);
			returns.setMsg("非管理员不可登陆！");
		} else {
			System.out.println("已经注册过了");
			User user = service.getUserInfo(user_account);

			String user_password2 = user.getUser_password();
			if (user_password2.equals(user_password)) {
				System.out.println("用户名和密码正确，返回用户信息");
 				request.getSession().setAttribute("admin", "admin");  // 设置
				Return_User users = new Return_User();
				users.setUser_uid(user.getUser_uid());
				users.setUser_name(user.getUser_name());
				request.getSession().setAttribute("loginUser", user.getUser_name());
				users.setUser_account(user.getUser_account());
				returns = new com.zhi.pojo.Return();
				returns.setCode(10001);
				returns.setIssuccess(true);
				returns.setMsg("登录成功");
				returns.setData(users);
			} else {
				System.out.println("用户名和密码不正确");
				returns = new com.zhi.pojo.Return();
				returns.setCode(10002);
				returns.setIssuccess(false);
				returns.setMsg("用户名和密码不正确！");

			}

 	}

		return returns;
	}





//	/**
//	 * 用户退出
//	 *
//	 * @param request
//	 * @param region
//	 * @return
//	 * @throws IOException
//	 */
//	// http://192.168.10.37/Shopping_2017/Photo/banner
//	@RequestMapping(value = "/logout")
//	@ResponseBody
//	private com.zhi.pojo.Return logout(HttpServletRequest request
//	)
//			throws IOException {
//		//session销毁
//		request.getSession().invalidate();
//		//跳一个页面
//
//		return returns;
//	}

}